package cn.ycc1.boot3ttl.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * @author ycc
 * @date 2024/9/22
 * TIMESTAMP 会自动转换日期格式
 * idea调试显示的 T 是标准的日期和时间分割符
 * SHOW VARIABLES LIKE '%time_zone%' 查看mysql时区的设置
 */
@RequiredArgsConstructor
@RestController
public class TestController {

    private final Executor myCompletableTaskExecutor;

    @GetMapping("/test")
    public String test() {

        myCompletableTaskExecutor.execute(() -> System.out.println(Thread.currentThread().getName()));
        myCompletableTaskExecutor.execute(() -> System.out.println(Thread.currentThread().getName()));
        return "hello ttl";
    }

    @GetMapping("/test2")
    public String test2() {

        test3(
                () -> System.out.println(Thread.currentThread().getName()),
                () -> System.out.println(Thread.currentThread().getName())
        );
        return "hello test2";
    }

    public void test3(Runnable... tasks) {
        CompletableFuture<?>[] futures = new CompletableFuture[tasks.length];
        futures[0] = CompletableFuture.runAsync(tasks[0], myCompletableTaskExecutor);
        futures[1] = CompletableFuture.runAsync(tasks[1], myCompletableTaskExecutor);
        System.out.println("test task...");

//        for(int i = 0; i < futures.length; i++) {
//            futures[i].join();
//        }

//        CompletableFuture completableFuture = CompletableFuture.allOf(futures);
//        completableFuture.join();
        System.out.println("test task end...");
    }
}
